package com.wyl.auth.service.impl;

import cn.hutool.core.util.StrUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;

import com.wyl.auth.entity.UserOperationLog;
import com.wyl.auth.mapper.UserOperationLogMapper;
import com.wyl.auth.req.UserOperationLogReq;
import com.wyl.auth.service.UserOperationLogService;
import com.wyl.auth.util.BeanAwareUtil;
import com.wyl.auth.vo.UserOperationLogVo;
import com.wyl.common.CommonPage;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.stream.Collectors;

/**
* @Description 用户操作日志
* @Author WuYiLong
* @Date 2025-04-16
*/
@Service
public class UserOperationLogServiceImpl extends ServiceImpl<UserOperationLogMapper, UserOperationLog> implements UserOperationLogService {


    @Override
    public CommonPage<UserOperationLogVo> page(UserOperationLogReq req) {
        Page<UserOperationLog> page = new Page<>(req.getCurrentPage(), req.getPageSize());
        LambdaQueryWrapper<UserOperationLog> wrapper = Wrappers.lambdaQuery(UserOperationLog.class);
        wrapper.like(StrUtil.isNotBlank(req.getUsername()),UserOperationLog::getUsername, req.getUsername());
        wrapper.between(req.getStartTime() != null && req.getEndTime() != null,UserOperationLog::getCreateTime, req.getStartTime(), req.getEndTime());
        wrapper.orderByDesc(UserOperationLog::getCreateTime);
        page = page(page, wrapper);
        List<UserOperationLogVo> userOperationLogVos = page.getRecords().stream().map(m -> {
            UserOperationLogVo userOperationLogVo = new UserOperationLogVo();
            BeanAwareUtil.copyProperties(m, userOperationLogVo);
            return userOperationLogVo;
        }).collect(Collectors.toList());

        CommonPage<UserOperationLogVo> commonPage = new CommonPage<>(req.getCurrentPage(), req.getPageSize());
        commonPage.setList(userOperationLogVos);
        commonPage.setTotal((int)page.getTotal());
        return commonPage;
    }

    @Override
    public void deleteByIds(List<Long> ids) {
        removeByIds(ids);
    }
}
